package event_;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class BallMove  extends JFrame {
    MyPaner mp = null;
    public static void main(String[] args) {
        BallMove ballMove = new BallMove();
    }

    public BallMove(){
        mp=new MyPaner();
        this.add(mp);
        this.setSize(1000,750);
        this.addKeyListener(mp);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }
}
class MyPaner extends JPanel implements KeyListener {
    private int x=10;
    private int y=10;
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillOval(x,y,20,20);

    }
    //有字符输入
    @Override
    public void keyTyped(KeyEvent e) {

    }
    //当某键按下
    @Override
    public void keyPressed(KeyEvent e) {
        // System.out.println( KeyEvent.getKeyText(e.getKeyCode())+"被按下");
        if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            y++;
        } else if (e.getKeyCode() == KeyEvent.VK_UP) {
            y--;
        } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            x--;
        } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            x++;
        }
        this.repaint();
    }
    //当某键松开
    @Override
    public void keyReleased(KeyEvent e) {

    }
}
